/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aviso.property.entity;

import java.io.Serializable;
import java.sql.Date;
import java.util.Calendar;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

/**
 *
 * @author avgusti
 */
@Entity
public class PropertyValue implements Serializable {

    public static final String fill = "                                                                                                                                                                                                                                                              ";
    public static final int filllen = 16;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String stringvalue;
    @ManyToOne
    private Item item;
    @ManyToOne
    private Property property;
    private transient String value;
    private transient Double dvalue;
    private transient Date date;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public Property getProperty() {
        return property;
    }

    public void setProperty(Property property) {
        this.property = property;
    }

    public String getStringvalue() {
        return stringvalue;
    }

    public void setStringvalue(String stringvalue) {
        if (stringvalue == null) {
            this.stringvalue = "";
        } else {
            this.stringvalue = stringvalue;
        }
        setValue(this.stringvalue.trim());
    }

    public void setValue(String s) {
        if (s == null) {
            s = "";
        }
        value = s;
        stringvalue = s;
        if (property.getType() != Property.PropertyType.String && property.getType() != Property.PropertyType.Text) {
            stringvalue = fill.substring(0, filllen - s.length()).concat(s);
        }
    }

    public String getValue() {
        return value;
    }

    public Long parseLong() {
        return Long.parseLong(value);
    }

    public Long parseDate() {
        return Date.valueOf(value).getTime();
    }

    public Double parseDouble() {
        return Double.parseDouble(value);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PropertyValue other = (PropertyValue) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 17 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "PropertyValue{" + "id=" + id + ", stringvalue=" + stringvalue + ", item=" + item + ", property=" + property + '}';
    }
}
